Modules and Scope

Objectives


Discussion

Modules

 Scope

Declared Using Where it is declared What can access it
Dim Inside procedure Only code inside the procedure
Private Outside procedure Any code in that form or module
Public Outside procedure Any code in the entire project

Determining How to Declare a Variable

Referring to Public Variables and Procedures on Other Forms/Modules

frmX.variablename = "value"
module1.gX = 100

Back to top


Demonstration

1. Open your Unit3 project.  Add a module.  You can leave its name as module1.vb.  Add a form and name it frmScope. Make frmScope the startup form. 

In this demonstration we will use a module and a form. However, everything that is discussed applies to the relationship between two forms and between two modules.

2.  Add the following two variables and two subs to the module:

Private privateX As Int16 = 1
Public publicX As Int16 = 10

Private Sub privateHI()
   MessageBox.Show("hi from module 1")
End Sub

Public Sub publicHI()
   MessageBox.Show("Hi from module1")
End Sub

3. Now add a button to frmScope and go into the click event for the button.  Type "module1." in the code window.  Notice that VS prompts you with publicX and publicHI but not with the private variable and sub.  This is because publicX and publicHI are public and available to the whole project.  Variables and functions that are private are private to only the form or module and are not available to any other form or module.  Now finish the statement and add the following code to your click event:

module1.publicHI()
module1.publicX = 2
messagebox.show(module1.publicX)

4. Run your project and click the button. Hopefully two messageboxes come up.

5.  Now add the following to frmScope .  You should add the following code just under Inherits System.Windows.Forms.Form at the top of the code window.

Private privateX As Int16
Public publicX As Int16

6.  Inside the click event for the button, type the following code before module1.publicHI()  to assign values to these variables:

privateX = 10000 
publicX = 20000

7. Notice that even though we gave the variables the same names as the variables in  module1, there is no confusion since variables in another form or module must be prefaced with the name of the module or form. Also, both variables are accessible in the event sub since both public and private variables can be accessed in the form (or module) in which they were declared.

8. Now we will look at local variables declared inside a procedure.  Add a sub to frmScope and declare a variable inside it as follows:

Private Sub CheckLocal()
   dim localX As Int16
End Sub

9. Now try to assign a value to  this variable in the click event for the button by typing: localX = 100.  Notice that you can not reference this variable in another sub because it is local to the sub CheckLocal().

Back to top
Exercises

1.  Assume that you have a module called moduleX and a form called frmMain. The module moduleX includes the following:

Private x as String = "Hi"
Public y as Single = 6.02
Public Function GetX()
   return(x)
End Function
Public Sub SetX(ByVal sX as String)
   x = sX
End Sub
Private Sub DoubleY()
  y = 2 * y
End Sub

Write code to do the following in frmMain.  You should first write it on paper and then try it in code.

  1. Set the value of x to "Bye".
  2. Display the value of x in a messagebox.
  3. Set the value of y to 20.
  4. Display the value of y in a messagebox.
  5. Double the current value of y.

2.  Create a form called frmTest .  Modify your form so that it meets the following:

  1. It has a button called btnIncrease, a button called btnDecrease, and a label called lblCount.
  2. It has a variable called iCount that holds Integers and is accessible to all procedures on the form but not to other forms and modules.
  3. When the user clicks btnIncrease, the value of iCount should be increased by one and displayed in lblCount.
  4. When the user clicks btnDecrease, the value of iCount should be decreased by one and displayed in lblCount.
  5. How could you write this program without using the variable iCount?

3.  Create a new form.  Then write a program to behave the same as the program in #2 but do not include the button for decreasing the counter.  Also declare the counter variable inside of the click event for the button that increases the counter.  How must the variable be declared if you want its value to be retained even after the click event has been completed?  If later you wanted to add a button to decrease the counter variable, could you?

4.  Assume that there is a form with the following variables declared near the top of the form outside of all subs and functions:

Private iPriv As Int16 = 2
Public iPub As Int16 = 3

The following subs are defined on the form:

Private Sub Sub1()
   Dim iS1 As Int16
   iPub = 10
   iPriv = iPub * 2
End Sub
Public Sub Sub2()
   iPub = iPub * 2
   iPriv = 5
   iS1 = 100
End Sub

  1. Which line(s) of code in the subs will cause errors? Why?
  2. Which sub could be called in another form?
Back to top
Links & Help
Back to top